//
//  WD_GroupViewController.m
//  BUDING
//
//  Created by HanWoonYong on 16/4/12.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_GroupViewController.h"
#import "DCPicScrollView.h"
#import "WD_TopRingPictureModel.h"
#import "WD_HomeGroupTableViewCell.h"
#import "DCPicScrollView.h"
#import "WD_GroupModel.h"

@interface WD_GroupViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *topPicture;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation WD_GroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatorTableView];
    [self getTopPicture];
    [self getInfo];
}

- (void)getTopPicture {
    [JQAFNetTool GETNetWithUrl:@"http://pudding.cc/api/v1/config?fields=featured_banner&apiKey=yuki_android&deviceKey=ffffffff-aecb-dcd6-ffff-ffffeb6b6ed7&version=2.8.9&brand=samsung&model=SM-G906S&osv=4.4.2&os=Android&cpuArch=armv7l&timestamp=1459668505&channelId=Wandoujia&auth1=f8072fcc16a8b03c01f60165e5b6cb50" body:nil headerFile:nil response:JQJSON success:^(id result) {
        if (result) {
            self.topPicture = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = [result objectForKey:@"featured_banner"];
            for (NSDictionary *dic in array) {
                WD_TopRingPictureModel *topRingPictureModel = [[WD_TopRingPictureModel alloc] initWithDataSource:dic];
                
                [self.topPicture addObject:topRingPictureModel.imageUrl];
            }
            DCPicScrollView *picture = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, -64, self.view.frame.size.width, 150) WithImageUrls:self.topPicture];
            self.tableView.tableHeaderView = picture;
            
        }
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)getInfo {
    [JQAFNetTool GETNetWithUrl:@"http://bangumi.bilibili.com/api/bangumi_recommend?_device=android&_hwid=545766b48a49ae0d&appkey=c1b107428d337928&build=414000&cursor=-1&pagesize=10&platform=android&ts=1459779767000&sign=bc8545ee8cc612e2381e43d3da1546ae" body:nil headerFile:nil response:JQJSON success:^(id result) {
        if (result) {
            self.dataSource = [NSMutableArray arrayWithCapacity:0];
            NSMutableArray *array = [NSMutableArray arrayWithArray:[result objectForKey:@"result"]];
            for (NSDictionary *dictionary in array) {
                WD_GroupModel *groupModel = [[WD_GroupModel alloc] initWithDataSource:dictionary];
                [self.dataSource addObject:groupModel];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];

}

- (void)creatorTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 120) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[WD_HomeGroupTableViewCell class] forCellReuseIdentifier:@"group"];
    
    [self.view addSubview:self.tableView];
    
}

#pragma mark - TableViewDelegate

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 230;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WD_HomeGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"group"];
    WD_GroupModel *groupModel = self.dataSource[indexPath.row];
    cell.groupModel = groupModel;
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
